package com.houdoudou.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.houdoudou.bean.Financer;
import com.houdoudou.bean.Owner;

public class OwnerDao {

	 Connection conn=null;
	   
		public OwnerDao(Connection conn) {
			super();
			this.conn = conn;
		}
		
		public  boolean create( Owner owner ) throws SQLException{	
			boolean result=false;
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "insert  into owner (telnum,serviceTelnum,name,address,secret,isAuth,rate) values (?,?,?,?,?,?,?)";
		        	pstm=conn.prepareStatement(sql);
		        	pstm.setString(1, owner.getTelnum());
		        	pstm.setString(2, owner.getServiceTelnum());
		        	pstm.setString(3, owner.getName());
		        	pstm.setString(4, owner.getAddress());
		        	pstm.setString(5, owner.getSecret());
		        	pstm.setInt(6, owner.getIsAuth());
		        	pstm.setDouble(7, owner.getRate());
					pstm.executeUpdate();
					result=true;
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return result;
		}
		
		public  Financer delete( String telnum) throws SQLException{	
			Financer financer = new Financer();
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "delete  from owner where telnum="+telnum+"";		        	
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
					stm.executeUpdate(sql);
					
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return financer;
		}
		
		public  int update( Owner owner ) throws SQLException{	
			int result=0;
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "update owner set serviceTelnum = ?,name=?,address=?,secret = ?,isAuth = ?,rate=?  where telnum = ?";
		        	pstm=conn.prepareStatement(sql);	        	
		        	pstm.setString(1, owner.getServiceTelnum());
		        	pstm.setString(2, owner.getName());
		        	pstm.setString(3, owner.getAddress());
		        	pstm.setString(4, owner.getSecret());
		        	pstm.setInt(5, owner.getIsAuth());
		        	pstm.setDouble(6, owner.getRate());
		        	pstm.setString(7, owner.getTelnum());
					result=pstm.executeUpdate();
					
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return result;
		}
		
		public  Owner selectByTelnum( String telnum) throws SQLException{	
			Owner owner = new Owner();
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "select * from owner where telnum="+telnum+"";		        	
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
					rs=stm.executeQuery(sql);
					if(rs.next()){
						
						owner.setTelnum(rs.getString("telnum"));
						owner.setServiceTelnum(rs.getString("serviceTelnum"));
						owner.setName(rs.getString("name"));
						owner.setAddress(rs.getString("address"));
						owner.setSecret(rs.getString("secret"));
					    owner.setIsAuth(rs.getInt("isAuth"));
					    owner.setRate(rs.getDouble("rate")); 
					}
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return owner;
		}
	
		public List< Owner> selectAll( ) throws SQLException{	
			List< Owner> owners= new ArrayList<Owner>();
			  PreparedStatement pstm=null; 
			   Statement  stm=null;
			   ResultSet rs=null; 	
			   String sql=null;
		        try {
		        	sql = "select * from owner ";		        	
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
					rs=stm.executeQuery(sql);
					while(rs.next()){
						Owner owner = new Owner();
						owner.setTelnum(rs.getString("telnum"));
						owner.setServiceTelnum(rs.getString("serviceTelnum"));
						owner.setSecret(rs.getString("secret"));
						owner.setName(rs.getString("name"));
						owner.setAddress(rs.getString("address"));
						owner.setIsAuth(rs.getInt("isAuth"));
						owner.setRate(rs.getDouble("rate"));
						owners.add(owner);
					}
				} finally{
					ConnFactory.closeStatment(pstm, pstm, rs);
				}			
			return owners;
		}	
}
